﻿using AutoMapper;
using B.Z.BasicData.API.Read.DTO;
using B.Z.BasicData.Domain.Entity.Authorize;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.Z.BasicData.API.Read.Application.User
{
    public class UserQueryCommandHandler : IRequestHandler<UserQueryCommand, APIResult<APIPaging<UserDto>>>
    {
        private readonly IBaseRepository<T_User> userRepository;
        private readonly IBaseRepository<T_Province> provinceRepository;
        private readonly IBaseRepository<T_Contry> contryRepository;
        private readonly IBaseRepository<T_City> cityRepository;
        private readonly IBaseRepository<T_UserRole> urRepository;
        private readonly IBaseRepository<T_Role> roleRepository;
        private readonly IMapper mapper;

        public UserQueryCommandHandler(IBaseRepository<T_User> userRepository, IBaseRepository<T_Province> provinceRepository, IBaseRepository<T_Contry> contryRepository, IBaseRepository<T_City> cityRepository, IBaseRepository<T_UserRole> urRepository, IBaseRepository<T_Role> roleRepository, IMapper mapper)
        {
            this.userRepository = userRepository;
            this.provinceRepository = provinceRepository;
            this.contryRepository = contryRepository;
            this.cityRepository = cityRepository;
            this.urRepository = urRepository;
            this.roleRepository = roleRepository;
            this.mapper = mapper;
        }

        public Task<APIResult<APIPaging<UserDto>>> Handle(UserQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<UserDto>> result = new APIResult<APIPaging<UserDto>>();
            APIPaging<UserDto> paging = new APIPaging<UserDto>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            var userList = userRepository.GetAll().AsNoTracking();
            var list = (from a in userList
                       join b in provinceRepository.GetAll().AsNoTracking()
                       on a.T_Province equals b.Id
                       join c in cityRepository.GetAll().AsNoTracking()
                       on a.T_CityId equals c.Id
                       join d in contryRepository.GetAll().AsNoTracking()
                       on a.T_ContryId equals d.Id
                       select new UserDto
                       {
                           Id = a.Id,
                           CreateUserName = a.CreateUserName,
                           CreateTime = a.CreateTime,
                           UpdateUserName = a.UpdateUserName,
                           UpdateTime = a.UpdateTime,
                           UserName = a.UserName,
                           Password = a.Password,
                           Sex = a.Sex,
                           T_CityId = a.T_CityId,
                           T_Province = a.T_Province,
                           T_ContryId = a.T_ContryId,
                           DetailAddress = a.DetailAddress,
                           ProvinceName = b.ProvinceName,
                           CityName = c.CityName,
                           ContryName = d.ContryName,
                           RoleName = "",
                           DepartMentId=a.DepartMentId
                       }).ToList();
            foreach (var item in list)
            {
                var urList = urRepository.GetAll().Where(p=>p.Uid==item.Id).AsNoTracking();
                var roleList = roleRepository.GetAll().AsNoTracking();
                var lists = from a in urList
                            join b in roleList
                            on a.Rid equals b.Id
                            select new
                            {a,b };
                var s= lists.Select(p => p.b.RoleName);
                item.RoleName = string.Join("、", s);
                
            }

            paging.RecordCount = list.Count();
            paging.List = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = paging;
            return Task.FromResult(result);

        }
    }
}
